package com.guomw.report.web.controller;

import com.guomw.report.common.enums.AppCode;
import com.guomw.report.common.utils.ApiResult;
import com.guomw.report.service.entity.Report;
import com.guomw.report.service.repository.ReportRepository;
import com.guomw.report.service.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private ReportService reportService;


    @Autowired
    private ReportRepository reportRepository;

    @RequestMapping("/list")
    public String list() {
        return "report/list";
    }


    @RequestMapping("/listView")
    @ResponseBody
    public ApiResult listView(@RequestParam(required = false, defaultValue = "") String name,
                              @RequestParam(required = false, defaultValue = "1") Integer pageIndex) {

        return ApiResult.resultWith(AppCode.SUCCESS, reportService.searchList(name, pageIndex));
    }


    @RequestMapping("/edit/{id}")
    public String list(@PathVariable(name = "id", required = false) Long id, Model model) {
        if (id != null && id > 0) {
            model.addAttribute("report", reportRepository.findOne(id));
        } else {
            id = 0L;
            Report report = new Report();
            report.setId(0L);
            model.addAttribute("report", report);
        }
        return "report/edit";
    }


    @RequestMapping("/save")
    @ResponseBody
    public ApiResult save(Long id, String code, String version, String name
            , String template, String cover, String special, String theory, String overview, String reference
            , String checkResultNormalTip
            , String checkResultAbnormalTip
            , String checkResultSuggest, String checkResultAbnormalSuggest
            , String sampleAmount, String sampleType) {

        if (id != null && id > 0) {
            Report report = reportRepository.findOne(id);
            if (report != null) {
                report.setCode(code);
                report.setVersion(version);
                report.setName(name);
                report.setTemplate(template);
                report.setCover(cover);
                report.setSpecial(special);
                report.setTheory(theory);
                report.setOverview(overview);
                report.setReference(reference);
                report.setCheckResultNormalTip(checkResultNormalTip);
                report.setCheckResultAbnormalTip(checkResultAbnormalTip);
                report.setCheckResultSuggest(checkResultSuggest);
                report.setCheckResultAbnormalSuggest(checkResultAbnormalSuggest);
                report.setSampleAmount(sampleAmount);
                report.setSampleType(sampleType);
                reportRepository.save(report);
            }

        } else {

            List<Report> reportList = reportRepository.findReport(code, version);
            if (reportList != null && reportList.size() > 0) {
                return ApiResult.resultWith(AppCode.ERROR);
            }
            Report report = new Report();
            report.setCode(code);
            report.setVersion(version);
            report.setName(name);
            report.setTemplate(template);
            report.setCover(cover);
            report.setSpecial(special);
            report.setTheory(theory);
            report.setOverview(overview);
            report.setReference(reference);
            report.setCheckResultNormalTip(checkResultNormalTip);
            report.setCheckResultAbnormalTip(checkResultAbnormalTip);
            report.setCheckResultSuggest(checkResultSuggest);
            report.setCheckResultAbnormalSuggest(checkResultAbnormalSuggest);
            report.setSampleAmount(sampleAmount);
            report.setSampleType(sampleType);
            reportRepository.save(report);
        }

        return ApiResult.resultWith(AppCode.SUCCESS);
    }


}
